<template>
	<view class="warp">
		<view class="uni-margin-wrap">
			<view class="mydriver-list-top">
				<uni-segmented-control :current="current" :values="segmentedItems" :style-type="styleType" :active-color="activeColor" @clickItem="onClickItem" />
			</view>
			<view class="notice-list-body">
				<view class="uni-list">
					<block v-for="(value, index) in list" :key="index">
						<view class="uni-list-cell" hover-class="uni-list-cell-hover">
							<view class="uni-media-list">
								<image class="uni-media-list-logo" :src="value.logo.length > 0 ? value.logo : '../../../static/image/pic_errer.png'"></image>
					
								<view class="uni-media-list-body">
									<view class="uni-media-list-text-top">
										<text class="text">{{check.isNull(value.real_name) ? '****' : value.real_name}}</text>
									</view>
									<view class="uni-media-list-text-body">
										<view class="back-text">
											<text>{{ check.isNull(value.plate_number) ? '无车牌号' : value.plate_number }}</text> 
										</view>
										
										<block v-if="current == 0">
											<block v-if="value.is_star == 0">
												<view class="btn" @click="addStar(value.id)">
													<text>收藏</text>
												</view>
											</block>
											<block v-else>
												<view class="btn" @click="delStar(value.id)">
													<text>取消收藏</text>
												</view>
											</block>
										</block>
										<block v-else>
											<view class="btn" @click="delStar(value.id)">
												<text>取消收藏</text>
											</view>
										</block>
									</view>
									<view class="uni-media-list-text-bottom">
										<text class="text">电话：{{ check.isNull(value.mobile) ? '' : value.mobile}}</text>
									</view>
								</view>
							</view>
						</view>
					</block>
				</view>
			</view>
		</view>
		 <view v-if="list.length == 0 "><ns-empty :isIndex="false" text="暂无司机"></ns-empty></view>
	</view>
</template>

<script>
export default {
	mixins: [],
	data() {
		return {
			list: [],
			segmentedItems: ['常用司机', '收藏司机'],
			current: 0,
			activeColor: '#f39e00',
			styleType: 'text',
		};
	},
	
	onLoad(option) {
		this.getAddonsOftenDriversData();
	},
	methods: {
		addStar(e){
		 uni.showModal({
		 	title: '提示',
		 	content: '是否收藏该司机？',
		 	confirmText: '确定',
		 	success: res => {
		 		if (res.confirm) {
		 		 this.addonsAddStarDriverData(e);
		 		} else {
					
				}
		 	}
		 });
		},
		delStar(e){
	 		uni.showModal({
	 			title: '提示',
	 			content: '是否取消收藏',
	 			confirmText: '确定',
	 			success: res => {
	 				if (res.confirm) {
	 				 this.addonsDelStarDriverData(e);
	 				} else {}
	 			}
	 		}); 
		},
		onClickItem(e) {
			if (this.current !== e.currentIndex) {
				this.current = e.currentIndex
			}
			if (this.current == 1) {
				 this.getAddonsStarDriverListData();
			} else {
				 this.getAddonsOftenDriversData();
			} 
		},
		getAddonsOftenDriversData() {	
			this.http.sendRequest({
				api: this.api.sDriver.sAddonsOftenDriversApi,
				data: {},
				success: res => {
					if (res.success == 'true' && res.data) {
						this.list = res.data.list;
					}
				}
			});
		},
		getAddonsStarDriverListData() {
			this.http.sendRequest({
				api: this.api.sDriver.sAddonsStarDriverListApi,
				data: {},
				success: res => {
					if (res.success == 'true' && res.data) {
						this.list = res.data.list;
					}
				}
			});
		},
		addonsAddStarDriverData(e) {
			var data = {
				driver_id:e
			}
			this.http.sendRequest({
				api: this.api.sDriver.sAddonsAddStarDriverApi,
				data: data,
				success: res => {
					if (res.success == 'true' && res.data) {
						if (this.current == 1) {
							 this.getAddonsStarDriverListData();
						} else {
							 this.getAddonsOftenDriversData();
						} 
					}
				}
			});
		},
		addonsDelStarDriverData(e) {
			var data = {
				driver_id:e
			}
			this.http.sendRequest({
				api: this.api.sDriver.sAddonsDelStarDriverApi,
				data: data,
				success: res => {
					if (res.success == 'true' && res.data) {
						if (this.current == 1) {
							 this.getAddonsStarDriverListData();
						} else {
							 this.getAddonsOftenDriversData();
						} 
					}
				}
			});
		}
	}
};
</script>

<style lang="scss">
@import '../public/css/myDriver.scss';
</style>
